package leetcode.Y2024M5D27;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;

public class S150 {
    public int evalRPN(String[] tokens) {
        Deque<String> stack=new LinkedList<>();
        for (String token : tokens) {
            if (stack.isEmpty()){
                stack.push(token);
            }else {

                switch (token) {
                    case "+": {
                        Integer x = Integer.parseInt(stack.pop());
                        Integer y = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(x + y));
                        break;
                    }
                    case "-": {
                        Integer x = Integer.parseInt(stack.pop());
                        Integer y = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(y - x));
                        break;
                    }
                    case "/": {
                        Integer x = Integer.parseInt(stack.pop());
                        Integer y = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(y / x));
                        break;
                    }
                    case "*": {
                        Integer x = Integer.parseInt(stack.pop());
                        Integer y = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(x * y));
                        break;
                    }
                    default:
                        stack.push(token);
                        break;
                }
            }
        }
        return Integer.parseInt(stack.pop());
    }

    @Test
    public void test(){
        String[] tokens = {"2","1","+","3","*"};
        System.out.println(evalRPN(tokens));
    }

    @Test
    public void test1(){
        String[] tokens = {"4","13","5","/","+"};
        System.out.println(evalRPN(tokens));
    }
    @Test
    public void test2(){
        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        System.out.println(evalRPN(tokens));
    }
}
